<template>
  <div>
    <el-dialog :title="info.isadd?'轮播图添加':'轮播图修改'" :visible.sync="info.isshow">
      <el-form :model="form">
        <el-form-item label="标题" label-width="120px">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="120px">
          <el-upload class="avatar-uploader" action="#" :show-file-list="false" :on-change="upload">
            <img v-if="imgUrl" :src="imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="qx">取 消</el-button>
        <el-button type="primary" v-if="info.isadd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {reqbanneradd,reqbannerinfo,reqbanneredit} from "../../../request/api";
import { errorAlert, successAlert } from "../../../utils/alert";
import {mapActions,mapGetters} from "vuex"
import path from "path";
export default {
  props: ["info"],
  data() {
    return {
      imgUrl: "",
      form: {
        title: "",
        img:null,
        status: "2",
      },
    };
  },
  computed:{
    ...mapGetters({
      list:"banner/list"
    })
  },
  methods: {
    ...mapActions({
       reqlist: "banner/reqlist",
    }),
    //清空
    emity(){
      this. form= {
        pid: "",
        catename: "",
        img:null,
        status: "2",
      }
      this.imgUrl=""
    },
    qx() {
      this.info.isshow = false;
    },
    //上传图片
    upload(e) {
      let file = e.raw
      // console.log(file);
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件最大2M");
        return;
      }
      let imgArr = [".jpg", ".jpeg", ".png", ".gif"];
      let extname = path.extname(file.name);
      if (!imgArr.includes(extname)) {
        errorAlert("请上传正确的图片格式");
        return;
      }
      this.imgUrl=URL.createObjectURL(file);
      this.form.img=file
    },
    checkEmpty() {
      return new Promise((resolve, reject) => {
        if (this.form.title == "") {
          errorAlert("标题不能为空");
          return;
        }
        if (!this.form.img) {
          errorAlert("图片不能为空");
          return;
        }
        resolve();
      });
    },
    add(){
      this.checkEmpty().then(()=>{
        reqbanneradd(this.form).then(res=>{
        console.log(res);
        if(res.data.code==200){
          successAlert(res.data.msg)
          this.emity();
          this.qx()
          this.reqlist()
        }
      })
      })
    },
     getOne(id) {
      reqbannerinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form=res.data.list;
          this.form.id=id;
        
          this.imgUrl=this.$pre+this.form.img
        }
      });
    },
    update() {
      this.checkEmpty().then(()=>{
        reqbanneredit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.qx();
          this.emity();
         this.reqlist()
        }
      });
      })
    },
  },
};
</script>

<style scoped>
.avatar-uploader /deep/.el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>